package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsTopic;
import com.qf.service.DtsTopicService;
import com.qf.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Zwnan
 */
@RestController
@RequestMapping("/admin/topic")
@CrossOrigin
public class TopicController {

    @Autowired
    private DtsTopicService dtsTopicService;

    @GetMapping("/list")
    public Object topicList(@RequestParam(defaultValue = "1")Integer page,
                            @RequestParam(defaultValue = "20")Integer limit,
                            @RequestParam(defaultValue = "add_time")String sort,
                            @RequestParam(defaultValue = "desc")String order){

        IPage<DtsTopic> iPage = dtsTopicService.findPage(page,limit,sort,order);

        long total = iPage.getTotal();
        List<DtsTopic> records = iPage.getRecords();
        Map map = new HashMap();
        map.put("total", total);
        map.put("items", records);
        return ResponseUtil.ok(map);
    }

    @PostMapping("/create")
    public Object create(@RequestBody DtsTopic dtsTopic){

        dtsTopicService.insert(dtsTopic);

        return ResponseUtil.ok();
    }
    @PostMapping("/update")
    public Object update(@RequestBody DtsTopic dtsTopic){

        dtsTopicService.update(dtsTopic);

        return ResponseUtil.ok();
    }

    @PostMapping("/delete")
    public Object delete(@RequestBody DtsTopic dtsTopic){

        dtsTopicService.delete(dtsTopic);

        return ResponseUtil.ok();
    }


}
